package com.samsung.controller.vote;

import java.net.URLEncoder;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.samsung.common.util.BaseUtil;
import com.samsung.common.util.Constant;
import com.samsung.model.user.User;
import com.samsung.model.vote.VoteTopic;
import com.samsung.service.vote.TopicService;
import com.samsung.service.vote.VoteHistoryService;

@Controller
@RequestMapping("/vote/router")
public class RouterController extends MultiActionController {
	@Autowired
	private TopicService topicService;
	@Autowired
	private VoteHistoryService voteHistoryService;

	@RequestMapping("/route")
	public void route(HttpServletRequest request, HttpServletResponse response, String topicId) throws Exception {
		if (BaseUtil.isEmpty(topicId)) {
			throw new Exception("投票主题编号不能为空");
		}
		
		VoteTopic topic = topicService.getTopic(topicId);
		if(topic==null) {
			// >>>跳转到404页面>>>
			throw new Exception("投票主题不存在");
		}
		
		String ip = BaseUtil.getRemoteHost(request);
		if(ip == null) {
			// >>>无法获取IP时处理逻辑>>>
			throw new Exception("无法获取您的IP");
		}
		
		Object userObj = request.getSession().getAttribute(Constant.LOGIN_TOKEN);
		if(new Date().before(topic.getDeadline())) {
			//当前主题截止时间仍然可投票
			if(userObj==null) {
				//游客
				if(topic.getScope() == 0) {
					//所有人可投
					int cnt = voteHistoryService.getCountByUserAndIP(topicId, "", ip);
					//是否投过
					if(cnt < 1) {
						//游客未投过该开放主题
						response.sendRedirect(request.getContextPath()+"/vote/ctVoteDetail.html?topicId="+topicId+"&r="+Math.random());
					} else {
						//游客已投过该主题
						response.sendRedirect(request.getContextPath()+"/vote/ctVoteResult.html?topicId="+topicId+"&r="+Math.random());
					}
				} else {
					//游客不可投隐私主题
					// 如果验证失败 返回到登录界面
					response.setDateHeader("expries", -1);
					response.setHeader("Cache-Control", "no-cache");
					response.setHeader("Pragma", "no-cache");
					
					StringBuffer cb = request.getRequestURL();
					String parms = request.getQueryString();
					if(BaseUtil.isNotEmpty(parms)) {
						cb.append("?");
						cb.append(parms);
					}
					StringBuffer url = new StringBuffer(request.getContextPath());
					url.append("/user/loginPage.html");
					if(BaseUtil.isNotEmpty(cb.toString())) {
						url.append("?callback=");
						url.append(URLEncoder.encode(cb.toString(), "UTF-8"));
						url.append("&r=");
						url.append(Math.random());
					}
					
					response.sendRedirect(url.toString());
				}
				
			} else {
				//会员
				//是否投过
				User u = (User) userObj;
				int cnt = voteHistoryService.getCountByUserAndIP(topicId, u.getUid(), ip);
				//是否投过
				if(cnt < 1) {
					//会员未投过该主题
					response.sendRedirect(request.getContextPath()+"/center/vote/voteDetail.html?topicId="+topicId+"&r="+Math.random());
					return;
				} else {
					//会员已投过该主题
					response.sendRedirect(request.getContextPath()+"/center/vote/voteResult.html?topicId="+topicId+"&r="+Math.random());
					return;
				}
			}
		} else {
			//当前主题已过期，不能投票
			//跳转至统计页面
			if( userObj == null ){//游客跳转至游客结果页面
				response.sendRedirect(request.getContextPath()+"/vote/ctVoteResult.html?topicId="+topicId+"&r="+Math.random());
			}else{
				response.sendRedirect(request.getContextPath()+"/center/vote/voteResult.html?topicId="+topicId+"&r="+Math.random());
			}
		}
		
	}
}
